﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace Inovout.Models
{
    public class CompositeId : DynamicDictionary
    {
        public CompositeId()
            : this(new Dictionary<string, object>())
        {
        }
        //private readonly IDictionary<string, object> compositeIdKeyDictionary;
        public CompositeId(IDictionary<string, object> compositeIdKeyDictionary)
            : base((IDictionary)compositeIdKeyDictionary)
        {
            //this.compositeIdKeyDictionary = compositeIdKeyDictionary;
        }
        public override IEnumerable<string> GetDynamicMemberNames()
        {
            return base.Keys.Cast<string>();
        }
        public override bool Equals(object obj)
        {
            CompositeId that = obj as CompositeId;
            if (that == null || !base.Count.Equals(that.Count))
                return false;

            return ((IDictionary<string, object>)InnerDictionary).All(dic => dic.Value.Equals(that[dic.Key]));
        }
        public override int GetHashCode()
        {
            int hashcode = base.GetHashCode();
            bool isFirst = true;
            foreach (var dic in (IDictionary<string, object>)InnerDictionary)
            {
                if (isFirst)
                {
                    hashcode = dic.Value.GetHashCode();
                }
                else
                {
                    hashcode ^= dic.Value.GetHashCode();
                }
            }
            return hashcode;
        }

    }
}
